package v1

import (
	"code.oldboyedu.com/studygo/ginStsMgr/common"
	"code.oldboyedu.com/studygo/ginStsMgr/dao/stu"
	"code.oldboyedu.com/studygo/ginStsMgr/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

/*
	实现学生的增删改查
*/
// CreateStu 增加学生信息 不允许学生同名
func CreateStu(c *gin.Context) {
	var data model.Student
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": common.BindERROR,
			"msg":    common.GetCode(common.BindERROR),
			"err":    err.Error(),
		})
		return
	}
	code := stu.CheckName(data.Name)
	if code == common.SUCCESS {
		stu.CreateStu(&data)
	}
	if code == common.StuNameExist {
		code = common.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data":   data,
		"msg":    common.GetCode(code),
	})
}
func ViewStu(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageNum == 0 {
		pageNum = 1
	}
	data, code := stu.ViewStu(pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data":   data,
		"msg":    common.GetCode(code),
	})
}
func ViewStuId(c *gin.Context) {
	var data model.Student
	id, _ := strconv.Atoi(c.Param("id"))
	code := stu.CheckId(id, &data)
	if code == common.SUCCESS {
		stu.ViewStuId(id)
	}
	if code == common.ERROR {
		code = common.ViesStuIdFailed
	}
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"msg":    common.GetCode(code),
		"data":   data,
	})
}
func UpdateStu(c *gin.Context) {
	//id, _ := strconv.Atoi(c.Param("id"))
	//var data model.Student
	//err := c.ShouldBindJSON(&data)
	//if err != nil {
	//	c.JSON(http.StatusBadRequest, gin.H{
	//		"status": common.BindERROR,
	//		"msg":    common.GetCode(common.BindERROR),
	//	})
	//	return
	//}
	//code := stu.CheckName(data.Name)
	//if code == common.SUCCESS {
	//	stu.UpdateStu(id, &data)
	//}
	//if code == common.ERROR {
	//	code = common.UpdateStuFailed
	//}
	//c.JSON(http.StatusOK, gin.H{
	//	"status": code,
	//	"msg":    common.GetCode(code),
	//	"data":   data,
	//})
	id, _ := strconv.Atoi(c.Param("id"))
	var data model.Student
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"status": common.BindERROR,
			"msg":    common.GetCode(common.BindERROR),
		})
		return
	}

	code := stu.CheckName(data.Name)
	if code != common.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status": code,
			"msg":    common.GetCode(code),
		})
		return
	}

	code = stu.UpdateStu(id, &data)
	if code != common.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status": code,
			"msg":    common.GetCode(code),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"msg":    common.GetCode(code),
		"data":   data,
	})
}
func DelStu(c *gin.Context) {
	var data model.Student
	id, _ := strconv.Atoi(c.Param("id"))
	code := stu.ViewStuId(id)
	if code == common.SUCCESS {
		stu.DelStu(id, &data)
	}
	if code == common.ERROR {
		code = common.DelStuFailed
	}
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"msg":    common.GetCode(code),
	})
}
